﻿using System;
using System.Linq;
using System.Web.Mvc;
using Super.Infrastructure;
using Super.Entities;
using Super.DomainService;

namespace Super.Web.Areas.Admin.Controllers
{
    public class ShopPageController : BaseController
    {
        //
        // GET: /Admin/ShopPage/

        public ActionResult Index()
        {
            var page=Query.Get("page");
            if(string.IsNullOrEmpty(page)) page="1";

            var adp = new DbContextAdapter();
            var shopPageService = new BaseService<ShopPage>(adp);
            var where = PredicateBuilder.True<ShopPage>();

            where = where.And(a => a.IsDel == 0);

            var shopPageList = shopPageService.Repository.Find(where,a=>a.Shop).OrderByDescending(a => a.ID).Page(page.ToInt(0), PageSize);
            BindPage(shopPageList.ItemCount, shopPageList.PageIndex, shopPageList.PageSize);

            return View(shopPageList.ItemCollection.ToList());
        }

        //
        // GET: /Admin/ShopPage/Delete/5
 
        public ActionResult Delete(int id)
        {
            var adp = new DbContextAdapter();
            var shopPageService = new ShopPageService(adp);
            shopPageService.Delete(id);
            adp.SaveChanges();
            PageTip(adp.ErrorMessage == null ? "删除成功！" : "删除失败！");

            return RedirectToAction("Index");
        }

        public JsonResult BatchRemove(string val)
        {
            var adp = new DbContextAdapter();
            var shopPageService = new ShopPageService(adp);
            string[] item = val.Substring(0, val.Length - 1).Split(',');
            foreach (string t in item)
            {
                shopPageService.Delete(int.Parse(t));
            }
            adp.SaveChanges();
            var action = adp.ErrorMessage == null;
            return Json("{'action':'" + action + "'}", JsonRequestBehavior.AllowGet);
        }
    }
}
